--[[
客户端相关显示逻辑管理

sendMenuRightControl
管理客户端主菜单右边的按钮显示逻辑
这个按钮的开放是根据主线任务来的

sendTipTopControl
管理客户端右上角的按钮显示逻辑显示逻辑

sendRightPanelControl
管理客户端右边的面板显示逻辑


sendFollowerNPCControl
管理跟随的npc
--]]

local ExcelMapConf = require("excel.Map")
local ExcelTaskConf = require("excel.Task")
local ExcelNpc = require("excel.Npc").npc
local Msg = require("core.Msg")
local TaskDefine = require("task.Define")
local TaskHelp = require("task.TaskHelp")
local Copy = require("copy.CopyL")
local BeginStory = require("scene.BeginStory")
local Line1FnIconExcel = require("excel.Role").line1FnIcon
local MainMenuRightExcel = require("excel.Role").mainMenuRight
local PreknowExcel = require("excel.Role").preknow
local PopExcel = require("excel.Role").popTip
local Guide = require("task.Guide")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Config = require("Config")
local RoleFFI = require("role.RoleFFI")
local Banshu = require("common.Banshu")

function getMaxFinishMainTaskID(human)
    if human.maxFinishMainTask then
        return human.maxFinishMainTask
    end

    local taskIDMax = 0
    for i = 0, human.db.taskRecordLen - 1 do
        local record = human.db.taskRecord[i]
        if record.status == TaskDefine.TASK_FINISHED then
            local taskConfig = TaskHelp.getTaskConfig(record.taskid)
            if taskConfig and taskConfig.mainType == TaskDefine.TASK_TYPE_1_MAIN and taskIDMax < record.taskid then
                taskIDMax = record.taskid
            end
        end
    end
    human.maxFinishMainTask = taskIDMax
    return taskIDMax
end

function getMaxDoingMainTaskID(human)
    if human.maxDoingMainTask then
        return human.maxDoingMainTask
    end

    local taskIDMax = 0
    for i = 0, human.db.taskRecordLen-1 do
        local record = human.db.taskRecord[i]
        if record.status == TaskDefine.TASK_ACCEPTED then
            local taskConfig = TaskHelp.getTaskConfig(record.taskid)
            if taskConfig and taskConfig.mainType == TaskDefine.TASK_TYPE_1_MAIN and taskIDMax < record.taskid then
                taskIDMax = record.taskid
                break -- 因为同时在做的主线任务只会有一个 所以这里可以直接break
            end
        end
    end

    human.maxDoingMainTask = taskIDMax
    return taskIDMax
end

local function cmpBtn(a, b)
	return a.weight > b.weight
end

local tableBtn1
local tableBtn2
function getMenuRightControlList(human)	
	local taskIDMax = getMaxFinishMainTaskID(human)
	local count1 = 0
	local count2 = 0
	tableBtn1 = {}
	tableBtn2 = {}
	
	for id,conf in pairs(MainMenuRightExcel) do
		if taskIDMax >= conf.finishTaskID then
			if conf.weight > 100 then
				count1 = count1 + 1
				tableBtn1[count1] = {id = id, weight = conf.weight}
			else
				count2 = count2 + 1
				tableBtn2[count2] = {id = id, weight = conf.weight}
			end
		end
	end	
	table.sort(tableBtn1, cmpBtn)
	table.sort(tableBtn2, cmpBtn)
	
	return count1, tableBtn1, count2, tableBtn2
end

function sendMenuRightControl(human)
	local count1,list1,count2,list2 = getMenuRightControlList(human)
	local msgRet = Msg.gc.GC_ROLE_MENU_RIGHT_CONTROL
	for i = 1, count1 do
		msgRet.itemID1[i] = list1[i].id
	end
	msgRet.itemID1[0] = count1
	for i = 1, count2 do
		msgRet.itemID2[i] = list2[i].id
	end
	msgRet.itemID2[0] = count2
	Msg.send(msgRet, human.fd)
end

function sendTipTopControl(human)
    local msgRet = Msg.gc.GC_ROLE_TIP_TOP_CONTROL
    msgRet.leftTime = 0
    msgRet.totalTime = 0
    local count = 0
    local nowCopyID = Copy.isCopyBySceneID(human.scene_id)
    if nowCopyID then -- 在副本中 要显示离开副本按钮
        local copyConfig = ExcelMapConf.copy[nowCopyID]
        if copyConfig and copyConfig.exitBtnType ~= 0 then
            count = count + 1
            msgRet.itemID[count] = copyConfig.exitBtnType            
            if copyConfig.exitBtnTime == 0 then
                msgRet.leftTime = 0
                msgRet.totalTime = copyConfig.time
            else
                msgRet.leftTime, msgRet.totalTime = Copy.getLeftTime(nowCopyID, Copy.sceneID2Room[human.scene_id])
            end
        end
    end

    msgRet.itemID[0] = count

    Msg.send(msgRet, human.fd)
end

-- 注意 这些定义不要乱改 要和map.xlsx里面的定义一致
RIGHT_PANEL_TASK                    = 1	    -- 任务追踪
RIGHT_PANEL_COPY_AIM                = 2		-- 副本目标
RIGHT_PANEL_COPY_PATA               = 3		-- 爬塔
RIGHT_PANEL_TD                      = 4     -- 塔防
RIGHT_PANEL_WORLDBOSS_RNAKLIST      = 5     -- 世界BOSS伤害排名榜
RIGHT_PANEL_WSSL                    = 6     -- wwsl
RIGHT_PANEL_JZWAR                   = 7     -- 荆州争夺战
RIGHT_PANEL_DATI                    = 8     -- 答题
RIGHT_PANEL_HORSERACE               = 9     -- 赛马
RIGHT_PANEL_FAMILYWAR               = 12    -- 军团战场
RIGHT_PANEL_DHFIGHT                 = 13    -- 大汉格斗场
RIGHT_PANEL_CCJJWAR                 = 14    -- 草船借箭

function sendRightPanelControl(human)
    local msgRet = Msg.gc.GC_ROLE_RIGHT_PANEL_CONTROL

    msgRet.rightPanel = RIGHT_PANEL_TASK

    local nowCopyID = Copy.isCopyBySceneID(human.scene_id)
    --由于地图有复用同一个scene，所以优先判断是否副本
    if nowCopyID then
        local copyConfig = ExcelMapConf.copy[nowCopyID]
        msgRet.rightPanel = copyConfig.rightPanel
    end

    Msg.send(msgRet, human.fd)
end

function sendFollowerNPCControl(human)
    local msgRet = Msg.gc.GC_ROLE_FOLLOWER_NPC_CONTROL

    -- 副本里面没有跟随npc
    local nowCopyID = Copy.isCopyBySceneID(human.scene_id)
    if nowCopyID and BeginStory.hasFollowNpcCopy(nowCopyID) == nil then
        msgRet.maxFinishMainTaskID = getMaxFinishMainTaskID(human)
		msgRet.maxDoingMainTaskID = getMaxDoingMainTaskID(human)
        
        msgRet.followers[0] = 0
        Msg.send(msgRet, human.fd)
        return
    end

    local taskIDMax = getMaxDoingMainTaskID(human)
    local taskStatus = TaskDefine.TASK_ACCEPTED
    if taskIDMax == 0 then
        taskIDMax = getMaxFinishMainTaskID(human)
        taskStatus = TaskDefine.TASK_FINISHED
    end

    msgRet.maxFinishMainTaskID = getMaxFinishMainTaskID(human)
    msgRet.maxDoingMainTaskID = getMaxDoingMainTaskID(human)
    if taskIDMax == 0 then
        msgRet.followers[0] = 0
    else
        local taskConfig = ExcelTaskConf.Task[taskIDMax]
        local virtualNpc = nil
        if taskStatus == TaskDefine.TASK_FINISHED then
            virtualNpc = taskConfig.virtualNpc2
        else
            virtualNpc = taskConfig.virtualNpc1
        end

        msgRet.followers[0] = #virtualNpc
        for index, npcID in ipairs(virtualNpc) do
            if ExcelNpc[npcID] == nil then
                assert(nil, "npc not exist:" .. npcID)
            end
            msgRet.followers[index].name = ExcelNpc[npcID].name
            msgRet.followers[index].npc_id = npcID
            msgRet.followers[index].body_id = ExcelNpc[npcID].body_id
            msgRet.followers[index].head_id = ExcelNpc[npcID].head_id
        end
    end


    Msg.send(msgRet, human.fd)
end

function canBanshuShow()
  if Banshu.IS_BANSHU then
    return false
  end
  return true
end

local extendCondition = {}
function sendLine1FnIcon(human)
    if extendCondition[10] == nil then
      extendCondition[10] = require("fuli.FirstPayLogic").canShowFirstReward 
      extendCondition[13] = require("activity.operate.DailyGift").canShow
      extendCondition[18] = require("activity.OperateActFL").canShow
      extendCondition[22] = require("activity.ComplexSrvActFL").canShow
      extendCondition[23] = require("activity.DateActFL").canShow
      extendCondition[20] = require("yellow.TGPLogic").canShow
      extendCondition[24] = require("yellow.TGPLogic").canActShow
      extendCondition[21] = require("activity.operate.ChongzhiPresent").canShow
      
      extendCondition[9] = canBanshuShow
      extendCondition[12] = canBanshuShow
      extendCondition[13] = canBanshuShow
      extendCondition[17] = canBanshuShow
    end
    local msgRet = Msg.gc.GC_LINE1_FN_ICON_LIST
    msgRet.icons[0] = 0
    for id,conf in pairs(Line1FnIconExcel) do
        if ((conf.lv ~= 0 and human.db.lv >= conf.lv) or (conf.taskID ~= 0 and getMaxFinishMainTaskID(human) >= conf.taskID))
            and (not Config.IS_MIDDLE or conf.middle == 1 )
            and (not extendCondition[id] or extendCondition[id](human, id)) then
                msgRet.icons[0] = msgRet.icons[0] + 1
                local left = msgRet.icons[msgRet.icons[0]]
                left.id = id
                left.line = conf.line
                left.icon = conf.icon
                left.priority = conf.priority
                left.arg[0] = 0
        end
    end
    Msg.send(msgRet,human.fd)
end


function sendPreknow(human)
    local msg = Msg.gc.GC_PREKNOW_INFO
    msg.info[0] = 0
    for lv, conf in pairs(PreknowExcel) do
        if human.db.lv >=lv and human.db.lv < conf.openlv then
            msg.info[1].openlv = conf.openlv
            msg.info[1].name =conf.name
            msg.info[1].tips = conf.tips
            msg.info[1].icon = conf.icon
            msg.info[0] = 1
            break
        end
    end
    Msg.send(msg, human.fd)
end

function onLevelUp(human, oldLv, newLv)
    sendLine1FnIcon(human)
    for id,conf in pairs(Line1FnIconExcel) do
        if oldLv < conf.lv and newLv >= conf.lv then
            local msgRet = Msg.gc.GC_LV_UP_GUIDE
            msgRet.guideID = conf.guideID
            Guide.setGuideList(conf.guideID,msgRet.guideList)
            Msg.send(msgRet,human.fd)
            break
        end
    end
    for lv, conf in pairs(PreknowExcel) do
        if oldLv < conf.openlv and newLv >= conf.openlv then
            Broadcast.sendBroadcast(human, Lang.GROSSIP, conf.desc1)
            local mm = Msg.gc.GC_SHOW_TXT
            mm.txt = conf.desc2
            Msg.send(mm, human.fd)
            break
        end
    end
    sendPreknow(human)
--    sendLvUpPop(human, oldLv, newLv)
end

function sendLvUpPop(human, oldLv, newLv)
    if oldLv < 30 and 30 <= newLv or oldLv < 35 and 35 <= newLv then
        local conf 
        for k,v in ipairs(PopExcel) do
            if (v.startSvrIndex == 0 or Config.SVR_INDEX >= v.startSvrIndex) and (v.endSvrIndex == 0 or Config.SVR_INDEX<=v.endSvrIndex)
                and (v.pf == "" or RoleFFI.ffi_string(human.db.pf) == v.pf) then
                conf = v
                break
            end
        end        
        if conf then
          local msg = Msg.gc.GC_LEVEL_UP_POP
          msg.url = conf.url
          msg.qq = conf.qq
          msg.mobile = conf.mobile
          Msg.send(msg, human.fd)
        end
    end
end
